<?php

	define("USER_TIMEOUT", 18000); /*10 minutos*/
	
	DEFINE ('DB_USER', 'root');
	
	if($_SERVER['SERVER_NAME']=="localhost")
	{
		DEFINE ('DB_PASSWORD', '123456');
	}
	else
	{
		DEFINE ('DB_PASSWORD', '123456');
	}
	DEFINE ('DB_HOST', 'localhost');
	DEFINE ('DB_NAME', '212850_galander');

	function begin()
	{
		@mysql_query("BEGIN");
	}
	
	function commit()
	{
		@mysql_query("COMMIT");
	}
	
	function rollback()
	{
		@mysql_query("ROLLBACK");
	}
	
	function openDB($con)
	{
		$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die ( 'Could not connect to MySQL: ' . mysql_error() );
		
		mysql_select_db(DB_NAME) or die ( 'Could not select the database: ' . mysql_error() );
		
		return $con;
	}
	
	function openDB2($con)
	{
		$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die ( 'Could not connect to MySQL: ' . mysql_error() );
		
		mysql_select_db(DB_NAME.'2') or die ( 'Could not select the database: ' . mysql_error() );
		
		return $con;
	}
	
	function closeDB($con)
	{
		mysql_close($con);
	}
	
	function executeSQL($mySQL)
	{
		$con = openDB($con);

		mysql_query($mySQL) or die ( 'Could not execute query: ' . mysql_error() );

		closeDB($con);
	}
	
	function executeSQL2($mySQL)
	{
		$con = openDB2($con);

		mysql_query($mySQL) or die ( 'Could not execute query: ' . mysql_error() );

		closeDB($con);
	}
	
	function transactionSQL($mySQL, $openAndClose)
	{
		$query = strtok($mySQL, ";");
		if($openAndClose)
		{
			$con = openDB($con);
		}
		begin();
		while ($query != false) 
		{
			mysql_query($query);
			$query = strtok(";");
			if(mysql_error())
			{
				break;	
			}
		}
		if(mysql_error())
		{
			rollback();
			$resultado = false;
		}
		else
		{
			commit();
			$resultado = true;
		}
		if($openAndClose)
		{
			closeDB($con);
		}
		return $resultado;
	}
	
	function readSQL($mySQL)
	{
		$arr = array();		
		$con = openDB($con);
		
		mysql_query("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
		$query = strtok($mySQL, ";");

		while ($query != false) 
		{
			$result = mysql_query($query);
			$query = strtok(";");
			if(mysql_error())
			{
				break;	
			}
		}
		
		$arr =mysql_fetch_rowsarr($result);
		
		mysql_free_result($result);
		
		closeDB($con);
		
		return $arr;	
	}
	
	function readSQL2($mySQL)
	{
		$arr = array();		
		$con = openDB2($con);
		
		$query = strtok($mySQL, ";");

		while ($query != false) 
		{
			$result = mysql_query($query);
			$query = strtok(";");
			if(mysql_error())
			{
				break;	
			}
		}
		
		$arr =mysql_fetch_rowsarr($result);
		
		mysql_free_result($result);
		
		closeDB($con);
		
		return $arr;	
	}
	
	function mysql_fetch_rowsarr($result, $numass=MYSQL_BOTH) 
	{
		$got = array();

  		if(mysql_num_rows($result) == 0)
    		return $got;

  		mysql_data_seek($result, 0);
  
  		while ($row = mysql_fetch_array($result, $numass)) 
  		{
    		array_push($got, $row);
  		}
  
  		return $got;
	}
?>
